﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using static System.Console;

namespace Professional
{
    public class FileAndStream
    {
        public void DriveInformation()
        {
            DriveInfo[] _Drives = DriveInfo.GetDrives();
            foreach (var item in _Drives)
            {
                if (item.IsReady)
                {
                    WriteLine($"Drive name : {item.Name}");
                    WriteLine($"Format : {item.DriveFormat}");
                    WriteLine($"Type : {item.DriveType}");
                }
            }
        }

        public void PathDemo()
        {
            WriteLine(Path.Combine(@"D:\Projects","Readme.txt"));
        }

        public void FileDemo()
        {
            File.ReadAllText("Path");
            File.ReadLines("");
            File.ReadAllLines("Path");


            File.WriteAllText(Path.Combine(@"C:\","Readme.txt"),"Hello World !!!");


            //执行时间较长
            FileInfo fileInfo = new FileInfo("源文件");
            WriteLine($"Name : {fileInfo.Name}");
            WriteLine($"Directory ; {fileInfo.DirectoryName}");

            fileInfo.CreationTime = new DateTime(2023, 12, 24, 15, 0, 0);

            fileInfo.CopyTo("目标文件", true);

            File.Copy("源文件","目标文件");


        }

        public void DirectoryDemo()
        {
            IEnumerable<string> _fileNames = Directory.EnumerateFiles("Path");
            foreach (var item in _fileNames)
            {
                Path.GetFileNameWithoutExtension("item");
                Path.ChangeExtension("Path",".txt");
            }
        }

        public void StreamDemo()
        {
            string _Path = @"C:\Users\Chen\Desktop\11.txt";
            FileStream _FileStream = new FileStream(_Path,FileMode.Open,FileAccess.Read,FileShare.Read);
            byte[] bom = new byte[5];

            _FileStream.Read(bom, 0, 5);

            byte [] preamble =  Encoding.UTF8.GetPreamble();

            _FileStream.Write(preamble,0,preamble.Length);

            preamble = Encoding.UTF8.GetPreamble();

            _FileStream.Write(preamble, 0, preamble.Length);

            if (bom == null)
            {

            }
        }

    }
}
